import datetime
import time

from flask import Blueprint, jsonify, g
from flask_restful import Api, Resource, reqparse, abort, marshal, fields


import datetime
from apps.models.touzhu_model import Pour

from apps.utils.decorator import login_required
from exts import db
from apps.models.heyue_model import Agreement, AgreementType

import datetime

from flask import Blueprint, jsonify, g
from flask_restful import Api, Resource, reqparse, abort, marshal, fields


from apps.models.moey_model import WalletMoney

from apps.utils.decorator import login_required
from exts import db
from apps.models.heyue_model import Agreement, AgreementType

touzhu_bp = Blueprint('touzhu', __name__, url_prefix='/touzhu')

api = Api(touzhu_bp)

parser = reqparse.RequestParser()

parser.add_argument('title', type=int, required=True, help='必须输入合约id')
parser.add_argument('jine', type=int, required=True, help='必须输入下注金额')
parser.add_argument('dizhi', type=str, required=True, help='必须输入充值地址')
parser.add_argument('win',type=int,required=True, help='必须下注')

touzhu_fileds = {
    # 合约标题
    'id': fields.Integer,
    # 下注时间（自动添加）
    'add_time': fields.DateTime,
    'money': fields.Integer,
    'win_and_lowdb': fields.Integer
}
agreement_fileds = {
    'title': fields.String,

}


class XiazhuResource(Resource):
    @login_required
    def post(self):
        user = g.user

        if user:
            args = parser.parse_args()
            h_id = args.get('title')
            dizhi = args.get('dizhi')
            jine = args.get('jine')
            win = args.get('win')
            time_now = datetime.datetime.now()
            heyue = Agreement.query.filter(Agreement.id == h_id and Agreement.end_time>time_now ).first()
            if heyue:  # 判断合约存在
                if heyue.status == 1:
                    money = WalletMoney.query.filter(WalletMoney.money_localtion == dizhi).first()
                    if money:  # 钱包地址是否存在
                        if money.moneyA < jine:  # 余额
                            abort(400, msg='现有A币不足')
                        else:
                            money.moneyA -= jine
                            db.session.add(money)
                            db.session.commit()

                            touzhu = Pour()
                            touzhu.user_id = user.id
                            touzhu.agree_id = h_id
                            touzhu.money = jine
                            touzhu.win_and_lowdb = win
                            db.session.add(touzhu)
                            db.session.commit()
                            data = {
                                'status': 200,
                                'msg': '合约下注成功',
                                'xiazhu': marshal(touzhu, touzhu_fileds),
                                'agrement':marshal(heyue, agreement_fileds)
                            }

                            return jsonify(data)
                    else:
                        abort(401,msg='充钱地址有误')
                else:
                    abort(403,msg='合约不在可投注状态')
            else:
                abort(402,msg='该合约不存在')


api.add_resource(XiazhuResource, '/')






